﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Interactivity;
using System.Windows.Controls;
using System.Windows;
using System.Collections;

namespace XWord.WPF.Behaviors
{


    public class ListBoxSelectAllAction : TargetedTriggerAction<FrameworkElement>
    {


        public bool IsChecked
        {
            get { return (bool)GetValue(IsCheckedProperty); }
            set { SetValue(IsCheckedProperty, value); }
        }

        public static readonly DependencyProperty IsCheckedProperty =
            DependencyProperty.Register("IsChecked", typeof(bool),
            typeof(ListBoxSelectAllAction), new UIPropertyMetadata());

        public ListBox TargetElement
        {
            get { return (ListBox)GetValue(TargetElementProperty); }
            set { SetValue(TargetElementProperty, value); }
        }

        public static readonly DependencyProperty TargetElementProperty =
            DependencyProperty.Register("TargetElement", typeof(ListBox), typeof(ListBoxSelectAllAction), 
            new UIPropertyMetadata(new PropertyChangedCallback((e,a)=>{
                (a.NewValue as ListBox).SetValue(VirtualizingStackPanel.IsVirtualizingProperty, false);
            })));





        

        protected override void Invoke(object parameter)
        {
          
            if (IsChecked)
            {
                TargetElement.SelectAll();
            }
            else
            {
                TargetElement.UnselectAll();
            }
        }
    }
}
